# 编写数据统计的业务功能
from rest_framework.views import APIView
from rest_framework.response import Response
from apps.users.models import User
from datetime import date, timedelta
from rest_framework.permissions import IsAdminUser
from apps.goods.models import GoodsVisitCount


class UserTotalCountView(APIView):
    """
        统计用户总数
    """
    # 指定权限
    permission_classes = [IsAdminUser]

    def get(self, request):
        # 查询用户
        count = User.objects.filter(is_staff=False).count()
        # 获取当天时间
        date_data = date.today()
        return Response({
            'count': count,
            'date': date_data
        })


class UserDayIncrementView(APIView):
    """
            统计日增用户
        """
    # 指定权限
    permission_classes = [IsAdminUser]

    def get(self, request):
        # 获取当天时间
        date_data = date.today()
        # 查询当天注册了多少用户
        count = User.objects.filter(is_staff=False, date_joined__gte=date_data).count()
        return Response({
            'count': count,
            'date': date_data
        })


class UserDayActiveView(APIView):
    """
            统计日活用户
    """
    # 指定权限
    permission_classes = [IsAdminUser]

    def get(self, request):
        # 获取当天时间
        date_data = date.today()
        # 查询当天登录了多少用户
        count = User.objects.filter(is_staff=False, last_login__gte=date_data).count()

        return Response({
            'count': count,
            'date': date_data
        })


class UserDayOrdersView(APIView):
    """
            统计下单用户
        """
    # 指定权限
    permission_classes = [IsAdminUser]

    def get(self, request):
        # 获取当天时间
        date_data = date.today()
        # 查询当天下单了用户   下订单的时间和当天日期比对 获取订单表的create_time  orderinfo__create_time
        users = User.objects.filter(is_staff=False, orderinfo__create_time__gte=date_data)
        count = len(set(users))

        return Response({
            'count': count,
            'date': date_data
        })


class UserMonthIncrementView(APIView):
    """
        月增用户统计
    """

    def get(self, request):
        # 1、获取今天日期
        data_date = date.today()
        # 2、获取30天前的日期
        old_date = data_date - timedelta(30)
        # 3、从30天前开始进行遍历，遍历30次分别获取每天的注册用户数量
        data_list = []
        for i in range(0, 31):
            # 获取当天日期
            index_date = old_date + timedelta(i)
            # 获取下一天的日期
            next_date = old_date + timedelta(i + 1)
            count = User.objects.filter(is_staff=False, date_joined__gte=index_date, date_joined__lt=next_date).count()
            data_list.append({
                'count': count,
                'date': index_date
            })

        # 4、返回结果
        return Response(data_list)


class GoodsDayView(APIView):

    def get(self, request):
        # 获取当天日期
        data_date = date.today()
        # 根据当天日期查询商品分类访问量表
        goods = GoodsVisitCount.objects.filter(date=data_date)
        # 构造返回结果
        data_list = []
        for good in goods:
            data_list.append({
                'category': good.category.name,
                'count': good.count
            })

        # 结果返回
        return Response(data_list)


